package com.lzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lzx.service.EsService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.*;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.reindex.ReindexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lzx
 * @Date: 2021/3/25 14:53
 */
@Service
@Slf4j
public class EsServiceImpl implements EsService {
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    /**
     * 判断索引是否存在
     * @return
     */
    @Override
    public Boolean isIndexExists(Class index){
        IndexOperations indexOperations = restTemplate.indexOps(index);
        return indexOperations.exists();
    }

    /**
     * 创建索引
     *
     * @param index
     * @param numberOfShards
     * @param numberOfReplicas
     * @return
     */
    @Override
    public Boolean createIndexWithShards(Class index, Integer numberOfShards, Integer numberOfReplicas) {
      return null;
    }

    /**
     * 创建索引(默认1个分片，1个副本)
     *
     * @param index
     * @return
     */
    @Override
    public Boolean createIndex(Class index) {
        if(isIndexExists(index)){
            log.info("索引已存在"+index.getName());
            return false;
        }
        IndexOperations indexOps = restTemplate.indexOps(index);
        indexOps.create();
        Document mapping = indexOps.createMapping();
        indexOps.putMapping(mapping);
        return true;
    }

    /**
     * 删除索引
     *
     * @param indexName
     * @return
     * @throws IOException
     */
    @Override
    public Boolean deleteIndex(String indexName) throws IOException {
        return null;
    }

    /**
     * 判断索引别名是否存在
     *
     * @param aliasName
     * @return
     */
    @Override
    public Boolean isAliasExists(String aliasName) throws IOException {
        return null;
    }

    /**
     * 创建索引同时给索引创建别名
     *
     * @param indexName
     * @param aliasName
     * @return
     */
    @Override
    public Boolean createIndexWithAlias(String indexName, String aliasName) throws IOException {
        return null;
    }

    /**
     * 给索引添加别名
     *
     * @param indexName
     * @param aliasName
     * @return
     * @throws IOException
     */
    @Override
    public Boolean addAlias(String indexName, String aliasName) throws IOException {
        return null;
    }

    /**
     * 删除某个索引的别名
     *
     * @param indexName
     * @param aliasName
     * @return
     * @throws IOException
     */
    @Override
    public Boolean deleteAlias(String indexName, String aliasName) throws IOException {
        return null;
    }

    /**
     * 重建索引，拷贝数据
     *
     * @param oldIndexname
     * @param newIndexname
     */
    @Override
    public void reindex(String oldIndexname, String newIndexname) throws IOException {

    }

    /**
     * 重建索引后修改别名
     *
     * @param aliasname
     * @param oldIndexname
     * @param newIndexname
     * @return
     */
    @Override
    public Boolean changeAliasAfterReindex(String aliasname, String oldIndexname, String newIndexname) throws IOException {
        return null;
    }

    /**
     * 添加mapping
     *
     * @param indexName
     * @param clazz
     * @return
     */
    @Override
    public Boolean addMapping(String indexName, Class<?> clazz) throws IOException {
        return null;
    }
}
